<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use  think\Request;
use think\Config;

class Daily extends Base
{
    
	public function Sign(){
	
		
		 return view();
	}
	public function getSign(){
		$where="sign.mid=".session("mid");

		$request = Request::instance();
		if($request->get('number')){
			$where=$where." and s.number like '%".$request->get('number')."%'";
		}
		if($request->get('truename')){
			$where=$where." and s.truename like '%".$request->get('truename')."%'";
		}
		if($request->get('classid')){
			$where=$where." and sign.classid=".$request->get('classid');
		}
		if($request->get('date1')&&$request->get('date2')){
			$where=$where." and  signdate between '".$request->get('date1')."' and '".$request->get('date2')."'";
		}
		//echo $where;

		$count =db('sign')->join(" student s"," s.id=sign.studentid")->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('sign')->join(" student s "," s.id=sign.studentid")->where($where)->order("sign.id desc")->limit($first,$_GET["size"])->field('sign.*,s.truename as studentname')->select();
		foreach ($list as $key => &$value) {
			
				$teacherinfo  =db('teacher')->where("id=".$value["teacherid"])->find();
				$value["teachername"]=$teacherinfo["teachername"];

		// 		$student  =db('student')->where("id=".$value["studentid"])->find();
		// 		$value["studentname"]=$student["truename"];

				$myclass  =db('myclass')->where("id=".$value["classid"])->find();
				$value["classname"]=$myclass["classname"];
		}
		if($count%$_GET["size"]==0){
			$obj["count"]=(int)($count/$_GET["size"]);
		}else{
			$obj["count"]=(int)($count/$_GET["size"])+1;
		}
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);


	}
	public function Leave(){
		
		
		
		
		
		 return view();
	}
	public function getLeave(){
		$where="leave.mid=".session("mid");

		$request = Request::instance();
		if($request->get('number')){
			$where=$where." and s.number like '%".$request->get('number')."%'";
		}
		if($request->get('truename')){
			$where=$where." and s.truename like '%".$request->get('truename')."%'";
		}
		if($request->get('classid')){
			$where=$where." and leave.classid=".$request->get('classid');
		}
		if($request->get('date1')&&$request->get('date2')){
			$where=$where." and  stime between '".$request->get('date1')."' and '".$request->get('date2')."'";
		}

		$count =db('leave')->join(" student s"," s.id=leave.sid")->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('leave')->join(" student s "," s.id=leave.sid")->where($where)->order("leave.id desc")->limit($first,$_GET["size"])->field('leave.*,s.truename as studentname')->select();
		foreach ($list as $key => &$value) {
			
				$teacherinfo  =db('teacher')->where("id=".$value["teacherid"])->find();
				$value["teachername"]=$teacherinfo["teachername"];

				$myclass  =db('myclass')->where("id=".$value["classid"])->find();
				$value["classname"]=$myclass["classname"];
				$value["stime"]=date("Y-m-d",strtotime($value["stime"])); 
				$value["etime"]=date("Y-m-d",strtotime($value["etime"])); 

			
		}
		if($count%$_GET["size"]==0){
			$obj["count"]=(int)($count/$_GET["size"]);
		}else{
			$obj["count"]=(int)($count/$_GET["size"])+1;
		}
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);


	}
	public function Video(){
		
		
		
		
		
		 return view();
	}
	public function getVideo(){
		$mid = session("mid");
		$where="mid=".$mid;
		$count =db('video')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('video')->where($where)->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
			$classid = $value["classid"];
			if($classid){
				$myclass = db("myclass")->where("id=".$classid)->find();
				if($myclass){
					$value["classname"]=$myclass["classname"];
				}

			}
		
		
	

			$onlinenum= db("useronline")->where("videoid=".$value["id"]." and  onlinetime>DATE_SUB(NOW(),INTERVAL  10 SECOND)")->count();
			$value["onlinenum"]=$onlinenum;
				
		}
		if($count%$_GET["size"]==0){
			$obj["count"]=(int)($count/$_GET["size"]);
		}else{
			$obj["count"]=(int)($count/$_GET["size"])+1;
		}
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);


	}
	public function videoshow(){
		$request = Request::instance();
		$mid = session("mid");
		if($request->get('id')){
			$info =db('video')->where("id=".$_GET["id"])->find();
			$mechanism = db("mechanism")->where("id=".$mid)->find();
			$sys_webconfig = db("sys_webconfig")->where("mid=0")->find();

			$time = date("Y-m-d H:i:s",time()+7200);
			$play = getplayurl2($sys_webconfig["cdn"], $info["videourl"], $sys_webconfig["playapikey"], $time);
			$info["url"]=$play[2];


			$this->assign('info',$info);
			
		}
		return view();
	}
	public function Videoadd(){
		$request = Request::instance();
	
	
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["title"] =$request->post('title');
				$data["desc"] =$request->post('desc');
				
				$data["seq"] =$request->post('seq');
				$data["createtime"] = date('Y-m-d H:i:s',time());
			
			
				$data["imgurl"] =$request->post('imgurl');
				$data["stime"] =$request->post('stime');
				$data["etime"] =$request->post('etime');
				$data["classid"] =$request->post('classid');
				
			if($data["id"]){
				
			
				
				if(db('video')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
			  $data["mid"] = session("mid");
			
			  if(db('video')->insertGetId($data)){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'修改失败'];
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('video')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
		
			
		
			
		}
		
		
	
	
		return view();
		
	}
	public function Videodel(){
		$request = Request::instance();
		$id =$request->post('id');
		
		
		 
			$where = 'id='.$id;
		  
		   $list=db('video')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}

	public function Work(){
		
		
		
		
		
		 return view();
	}
	public function getWork(){
		$mid = session("mid");
		$where="mid=".$mid;
		$count =db('work')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('work')->where($where)->order("id desc")->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
			
				//$teacherinfo  =db('teacher')->where("id=".$value["teacherid"])->find();
				//$value["teachername"]=$teacherinfo["teachername"];

				$classinfo  =db('myclass')->where("id=".$value["classid"])->find();
				$value["classname"]=$classinfo["classname"];

				$teacherinfo  =db('teacher')->where("id=".$value["teacherid"])->find();
				$value["teachername"]=$teacherinfo["teachername"];

				$result = Db::query('select studentid from workresult where workid=? group by studentid',[$value["id"]]);

				$value["fankuinum"]= count($result);

		}
		if($count%$_GET["size"]==0){
			$obj["count"]=(int)($count/$_GET["size"]);
		}else{
			$obj["count"]=(int)($count/$_GET["size"])+1;
		}

		
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);


	}
	public function Workadd(){
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["title"] =$request->post('title');
				$data["desc"] =$request->post('desc');
				$data["teacherid"] =$request->post('teacherid');
				$data["createtime"] = date('Y-m-d H:i:s',time());
				$data["overtime"] =$request->post('overtime');
				$data["classid"] =$request->post('classid');
				$data["teacherid"] =$request->post('teacherid');
				$data["imgurl"] =$request->post('imgurl');
				
				
			if($data["id"]){
				
			
				
				if(db('work')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				$data["mid"] = session("mid");
			
			  if(db('work')->insertGetId($data)){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'修改失败'];
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('work')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
		
			
		
			
		}
		
		
	
	
		return view();
		
	}
	public function Workdel(){
		$request = Request::instance();
		$id =$request->post('id');
		
		
		 
			$where = 'id='.$id;
		  $list=db('workresult')->where("workid=".$id)->delete();
		   $list=db('work')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	public function workresultdel(){
		$request = Request::instance();
		$id =$request->post('id');
			$where = 'id='.$id;
		   $list=db('workresult')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }

	}
	public function workresult(){
		$request = Request::instance();


		if($request->isPost()){
			$id =$request->post('id');
			$where="workid=".$id;
			$count =db('workresult')->where($where)->count();
			$first = $_POST["size"] * ($_POST["page"] -1);
			 
			$list = db('workresult')->where($where)->limit($first,$_POST["size"])->select();
			foreach ($list as $key => &$value) {
				
					//$teacherinfo  =db('teacher')->where("id=".$value["teacherid"])->find();
					//$value["teachername"]=$teacherinfo["teachername"];
	
					//$classinfo  =db('myclass')->where("id=".$value["classid"])->find();
					//$value["classname"]=$classinfo["classname"];
	
					$student  =db('student')->where("id=".$value["studentid"])->find();
					$value["studentname"]=$student["truename"];
					$value["imglist"]= explode(",",$value["imglist"]);
			}
			if($count%$_POST["size"]==0){
				$obj["count"]=(int)($count/$_POST["size"]);
			}else{
				$obj["count"]=(int)($count/$_POST["size"])+1;
			}
	
			$obj["allcount"]=$count;
			$obj["list"]=$list;
			return json($obj);
	

		}else{

			$id =$request->get('id');
			$this->assign('id',$id);
				
			return view();
		}
	

	}
	public function Food(){
		
		
		
		
		
		 return view();
	}
	public function getFood(){
		$mid = session("mid");
		$where="mid=".$mid;
		$count =db('food')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('food')->where($where)->order("id desc")->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
			
			$value["mydate"]=date_format(date_create($value["mydate"]),"Y-m-d");
			$result = Db::query('select avg(star) as avgnum from foodstar where foodid=?',[$value["id"]]);
			$value["avgnum"]= intval($result[0]["avgnum"]);
			
		}
		if($count%$_GET["size"]==0){
			$obj["count"]=(int)($count/$_GET["size"]);
		}else{
			$obj["count"]=(int)($count/$_GET["size"])+1;
		}
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);


	}
	public function Foodadd(){
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["title"] =$request->post('title');
				$data["imgurl"] =$request->post('imgurl');
				$data["desc"] =$request->post('desc');
				$data["mydate"] =$request->post('mydate');
				$data["type"] =$request->post('type');
				$data["star"] =$request->post('star');

				
				$data["createtime"] = date('Y-m-d H:i:s',time());
			
				
			if($data["id"]){
				
			
				
				if(db('food')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				$data["mid"] = session("mid");
			
			  if(db('food')->insertGetId($data)){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'修改失败'];
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('food')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
		
			
		
			
		}
		
		
	
	
		return view();
		
	}
	public function Fooddel(){
		$request = Request::instance();
		$id =$request->post('id');
		
		
		 
			$where = 'id='.$id;
		  
		   $list=db('food')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	public function News(){
		
		
		
		
		
		 return view();
	}
	public function getNews(){
		$mid = session("mid");
		$where="mid=".$mid;
		$count =db('news')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('news')->where($where)->order("id desc")->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
			
			
			
		}
		if($count%$_GET["size"]==0){
			$obj["count"]=(int)($count/$_GET["size"]);
		}else{
			$obj["count"]=(int)($count/$_GET["size"])+1;
		}
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);


	}
	public function Newsadd(){
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["title"] =$request->post('title');
				$data["desc"] =$request->post('desc');
				$data["content"] =$request->post('content');
				$data["author"] =$request->post('author');
				$data["createtime"] = date('Y-m-d H:i:s',time());
			
				$data["imgurl"] =$request->post('imgurl');
				
			if($data["id"]){
				
			
				
				if(db('news')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				
				$data["mid"] =session("mid");
			  if(db('news')->insertGetId($data)){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'修改失败'];
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('news')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
		
			
		
			
		}
		
		
	
	
		return view();
		
	}
	public function Newsdel(){
		$request = Request::instance();
		$id =$request->post('id');
		
		
		 
			$where = 'id='.$id;
		  
		   $list=db('news')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	
	public function messagedel(){
		$request = Request::instance();
		$id =$request->post('id');
		
		
		 
			$where = 'id='.$id;
		   $list = db("messagelist")->where("msgid=".$id)->delete();
		   $list=db('message')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	public function Message(){
		
		
		
		
		
		 return view();
	}
	public function getMessage(){
		
		$where="mid=".session("mid");
		$count =db('message')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('message')->where($where)->order("id desc")->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
			$users =  json_decode($value["users"],true);
			$value["users"]=$users;
			
			
		}
		if($count%$_GET["size"]==0){
			$obj["count"]=(int)($count/$_GET["size"]);
		}else{
			$obj["count"]=(int)($count/$_GET["size"])+1;
		}
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);


	}
	public function messageadd(){
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["title"] =$request->post('title');
				$data["desc"] =$request->post('desc');
				$data["users"] =$request->post('users');
				$data["sendusername"] =$request->post('sendusername');
				$data["createtime"] = date('Y-m-d H:i:s',time());
			
				$length = mb_strlen($data["desc"],'utf8');

				if($length>150){
					return ['status'=>500,'message'=>'消息内容不得超过150个字符'];
				}
			if($data["id"]){
				
			
			}else{
				
				$data["mid"] =session("mid");
				$mechanism = db("mechanism")->where("id=".$data["mid"])->find();
				$message =db('message')->insertGetId($data);
			  if($message){

				$users = json_decode($data["users"],true);
				for ($i = 0; $i < count($users); $i++)
				{
					$studentid = $users[$i]["id"];
					$studentandparent = db("studentandparent")->where("studentid=".$studentid)->find();
					if($studentandparent){
						$userid= $studentandparent["userid"];
						$user = db("users")->where("id=".$userid)->find();
						$openid = $user["openid"];
						$content =$data["title"];
						$arr = array();
						$arr[0]=$mechanism["name"];
						$arr[1]=$data["sendusername"];
						$arr[2]=date('Y-m-d H:i:s',time());
						$arr[3]=$data["desc"];

						$s1["userid"]=$users[$i]["id"];
						$s1["msgid"]=$message;
						$s1["createtime"] =date('Y-m-d H:i:s',time());
						$s1["state"]=0;
						db('messagelist')->insertGetId($s1);


						$str = sendtempmsg4("J3SiT1NyCn7XqiP5u50Y203ZSnwRvvlwjAIAw0kUEjw",$openid,$content,$arr);
					}
					
				

				}

					 return ['status'=>200,'message'=>'发送成功'];
					
				}else{
					return ['status'=>500,'message'=>'修改失败'];
				}
			}
			
		}else{
			
		
		
			return view();
		}
		
		
	
	
	
		
	}
	public function messageview(){

		$message =db('message')->where("id=".$_GET["id"])->find();
		
		$users = json_decode($message["users"],TRUE);

		$this->assign('users',$users);
		$this->assign('info',$message);
		return view();
	}
	
}
